package com.ease.concurrent.thread.state;

/**
 * 1. Sleep时处于什么状态？
 * 锁池状态 TIMED_WAITING
 *
 * @author: chenhs
 * @date: Created in 9:32 2019/12/27
 **/
public class ThreadStateDemo1_Sleep {

    static volatile boolean running = true;

    public static void main(String[] args) throws Throwable {

        Thread t1 = new Thread(() -> {

            try {
                while (running) {
                }
                System.out.println(" t1 running is false. t1将sleep了");
                Thread.sleep(10000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        /** NEW 初始状态 */
        System.out.println("new t1  t1的状态：" + t1.getState());

        t1.start();

        /** 主线程睡2秒，这时t1处于running */
        Thread.sleep(2000L);
        /** RUNNABLE 可运行或运行中 */
        System.out.println("t1.start()后的状态：" + t1.getState());

        /** 将控制标识设置为false，让子线程退出循环 */
        running = false;

        /**主线程再睡2秒保证t1这时进入的是sleep语句阶段*/
        Thread.sleep(2000L);
        /** TIMED_WAITING 锁池状态 */
        System.out.println("t1.sleep()时的状态：" + t1.getState());
    }


}
